'''
将大文件分割为小文件，按行进行切割
'''
import os

def divide_text_by_line(big_file, small_file_lines, new_file_path, times):
    if not os.path.exists(new_file_path):
        os.mkdir(new_file_path)
    
    new_file = os.path.join(new_file_path, os.path.splitext(os.path.basename(big_file))[0])
    print(new_file)
    
    with open(big_file, 'r', encoding='utf-8') as myfile:
        index = 0
        while index < times:
            index += 1
            save_file = new_file + '-%d' % index + os.path.splitext(os.path.basename(big_file))[1]
            print(save_file)
            try:
                with open(save_file, 'a+', encoding='utf-8') as f:
                    for _ in range(small_file_lines):
                        # print(str(myfile.readline()))
                        f.write(str(myfile.readline()))
            except StopIteration:
                break

def split(fromfile,todir,chunksize=15000000):
    if not os.path.exists(todir):#check whether todir exists or not
        os.mkdir(todir)
    else:
        for fname in os.listdir(todir):
            os.remove(os.path.join(todir,fname))
    partnum = 0
    inputfile = open(fromfile,'rb')#open the fromfile
    while True:
        chunk = inputfile.read(chunksize)
        if not chunk:             #check the chunk is empty
            break
        partnum += 1
        filename = os.path.join(todir,('part%04d'%partnum))
        fileobj = open(filename,'wb')#make partfile
        fileobj.write(chunk)         #write data into partfile
        fileobj.close()
    return partnum

def joinfile(fromdir,filename,todir):
    if not os.path.exists(todir):
        os.mkdir(todir)
    if not os.path.exists(fromdir):
        print('Wrong directory')
    outfile = open(os.path.join(todir,filename),'wb')
    files = os.listdir(fromdir) #list all the part files in the directory
    files.sort()                #sort part files to read in order
    for file in files:
        filepath = os.path.join(fromdir,file)
        infile = open(filepath,'rb')
        data = infile.read()
        outfile.write(data)
        infile.close()
    outfile.close()

if __name__=="_main__":
    # 大文件的绝对路径
    big_file = "D:/桌面/临时文件/0804/call_email.txt"
    # 切割后小文件的行数
    small_file_lines = 1000
    # 切割后小文件的存储的目录
    new_file_path = os.path.join(os.path.dirname(big_file),"small_file1")
    # 切割大文件时，只切割前times次的文件
    times = 30
    divide_text_by_line(big_file, small_file_lines, new_file_path, times)